package com.haogu.controller;

import com.haogu.entity.*;
import com.haogu.service.RoleService;
import com.haogu.service.UserService;
import com.haogu.util.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system")
public class UserManagerController {
    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    /**
     * 分页展示
     * @return
     */
    @RequestMapping("/toListUser")
    public String toListUser(){
        return "system/list_user";
    }

    @RequestMapping("/listUser")
    @ResponseBody
    public Map listUser(Integer page,Integer rows){
        Map map = new HashMap();
        int rows1 = userService.selectCount();
        List<VUser> vUsers = userService.selectAllUserByPage(page, rows);
        map.put("totalRows",rows1);
        map.put("result",vUsers);
        Map map1 = new HashMap();
        map1.put("data",map);
        return map1;
    }
    /**
     * 新建客户
     */
    @RequestMapping("/toAddUser")
    public String toAddUser(){
        return "system/add_user";
    }
    @RequestMapping("/addUser")
    @ResponseBody
    public Map addUser(User user, HttpSession session){
        User user1 = (User) session.getAttribute("user");
        user.setCreateBy(user1.getCreateBy());
        user.setUpdateBy(user1.getUpdateBy());
        boolean b = userService.insertUser(user);
        Map map = new HashMap();
        if(b){
            map.put("success",true);
        }else{
            map.put("success",false);
        }
        return map;
    }
    /**
     * 删除用户
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Map deleteUser(String ids){

        String[] split = ids.split(",");
        boolean flag = true;
        for(int i = 0; i < split.length;i++){
            boolean delete = userService.delete(Integer.parseInt(split[i]));
            if(!delete){
                flag = false;
            }
        }
        Map map = new HashMap();

        if(flag){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     *修改用户
     */
    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(Integer id, Model model){
        User userById = userService.findUserById(id);
        model.addAttribute("user",userById);
        return "system/edit_user";
    }
    /**
     *修改用户
     */
    @RequestMapping("/toUpdateUser1")
    public String toUpdateUser1(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        User userById = userService.findUserById(user.getId());
        model.addAttribute("user",userById);
        return "system/edit_user";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public ResultData updateUser(User user){
        boolean b = userService.update(user);
        Map map = new HashMap();
        if(b){
            map.put("success",true);
        }else{
            map.put("success",false);
        }
        ResultData resultData = new ResultData(b, null);

        return resultData;
    }

    /**
     * 权限分配
     */
    @RequestMapping("/toAssignUser")
    public String toAssignUser(Integer userId,Model model){
        model.addAttribute("userId",userId);
        return "system/assign_user";
    }
    
    @RequestMapping("/assignUser")
    @ResponseBody
    public Map assignUser(){
        List<Role> roles = roleService.selectAllRole();
//        List<User> users = userService.selectAllUser();
        Map map = new HashMap();
        map.put("data",roles);
        return map;
    }
    @RequestMapping("/byUserIdGetRoleInfo")
    @ResponseBody
    public Map byRoleIdAssigRole(Integer userId){
        List<Role> roles = userService.selectRoleInfoByUserId(userId);
        Map map = new HashMap();
        map.put("success",true);
        map.put("data",roles);
        return map;
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping("/toLogout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/login";
    }

}
